import React from "react";
import { Link, Routes, Route, useNavigate } from "react-router-dom";

const Home = () => {
  // 删除了useHistory，需要使用useNavigate来进行编程式导航
  const navigate = useNavigate();

  const fn = () => {
    // 直接调用写路径，相当于之前的push方法
    // navigate("/detail");

    // 加上第二个参数{ replace: true }，相当于之前的replace方法
    // navigate("/detail", { replace: true });

    // 直接里面写整数的，相当于go方法
    // navigate(-1);

    // 传入对象的形式
    navigate(
      {
        pathname: "/detail",
        search: "?a=4$b=4",
        hash: "#123",
      },
      {
        replace: true,
        state: {},
      }
    );
  };

  return (
    <div>
      这是首页 - <button onClick={fn}>btn</button>
    </div>
  );
};
const About = () => <div>这是关于页</div>;
const Detail = () => {
  return <div>这是详情页</div>;
};

const App = () => {
  return (
    <div>
      <h2>router-v6-编程式导航</h2>
      <ul>
        <li>
          <Link to="/">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail">详情页</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/" element={<Home></Home>}></Route>
        <Route path="/about" element={<About />}></Route>
        <Route path="/detail" element={<Detail />}></Route>
      </Routes>
    </div>
  );
};

export default App;
